﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fengsoft.AIBlog.Web.Code;
using Fengsoft.AIBlog.Web.Models;
using Fengsoft.FengCommerce.BusinessComponents;
namespace Fengsoft.AIBlog.Web.Controllers
{
    public class QuestionnaireController : ExtensionController
    {
        private FengCommerceServiceUnit _serviceUnit;
        public QuestionnaireController()
        {
            _serviceUnit=new FengCommerceServiceUnit();
        }
        //
        // GET: /Questionnaire/

        public ActionResult Index()
        {
            QuestionnaireIndexModel model=new QuestionnaireIndexModel();
            model.Questionnaire=new QuestionnairePartial();
            model.Questionnaire.Questionnaire=_serviceUnit.QuestionnaireService.GetQuestionnaireByReferenceName("MemberSatisfactionSurvey");
            return View(model);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(FormCollection form)
        {
            QuestionnaireCompleteModel model=new QuestionnaireCompleteModel();
            string[] validationMessages=null;
            string createdBy=string.Empty;
            if (User.Identity.IsAuthenticated)
            {
                createdBy=User.Identity.Name;
            }
            else
            {
                createdBy=Request.AnonymousID;
            }
            _serviceUnit.QuestionnaireAnswerService.AddQuestionnaireAnswers(form["ReferenceName"],Request.UserHostAddress,createdBy,form,out validationMessages);
            _serviceUnit.SaveChanges();
            model.AddValidationMessages(validationMessages);
            TempData["model"]=model;
            return RedirectToAction("complete");
        }
        public ActionResult Complete()
        {
            return View((QuestionnaireCompleteModel)TempData["model"]);
        }
        protected override void Dispose(bool disposing)
        {
            _serviceUnit.Dispose();
            base.Dispose(disposing);
        }
    }
}
